package com.zrc.superframe_net.converter.adapter

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import java.io.IOException

/**
 * @Author: superman
 * @CreateTime: 7/28/21
 * @Describe:
 */
class BooleanTypeAdapter : TypeAdapter<Boolean>() {
    @Throws(IOException::class)
    override fun read(reader: JsonReader): Boolean {
        return when (reader.peek()) {
            JsonToken.NULL -> {
                reader.nextNull()
                false
            }

            JsonToken.STRING -> {
                val s = reader.nextString()
                s == "true"
            }

            JsonToken.BOOLEAN -> reader.nextBoolean()

            else -> false
        }
    }

    @Throws(IOException::class)
    override fun write(writer: JsonWriter, value: Boolean) {
    }
}